#ifndef IMPORTEXPORT_H_
#define IMPORTEXPORT_H_

#include "../../core/Buddy.h"

#include <wx/string.h>
#include <wx/txtstrm.h>
#include <wx/datetime.h>
#include "../../core/interfaces/iModule.h"
#include "../../core/interfaces/iUserInterface.h"
#include "../../core/events/ImppCommandEvent.h"

#include "wx/xml2.h"                // include libxml2 wrapper definitions

namespace Impp
{

#define STD_IMPORT_EXPORT_MODULE_NAME 					wxT("StdImportExport")
#define STD_IMPORT_EXPORT_MODULE_ACTION_IMPORT			wxT("import")
#define STD_IMPORT_EXPORT_MODULE_ACTION_EXPORT_WHOLE	wxT("exportWhole")

class ImportExport : public wxEvtHandler
{
private:
	class HistoryRecord{
	public:
		bool out;
		wxDateTime time;
		wxString msg;
	};
	class BuddyRecord{
	public:
		Buddy* buddy;
	};

	class ImportSession{
	public:
		ImportSession() :
			id(-1),
			account(NULL),
			buddy(NULL)
			{};

		ActionId id;
		Account* account;
		wxXml2Document doc;
		wxXml2Node curBuddyNode;
		wxXml2Node curMessageNode;
		Buddy* buddy;
        size_t importedMessagesCount;
        size_t messageCount;
	};
private:
	ImportSession* importSession;

public:
        ImportExport();
        virtual ~ImportExport();

public:
        void exportBuddyHistory(Buddy* buddy, const wxString & file);
        void exportAllHistory(Account* acc, const wxString & file);

        void importHistory(Account* account, const wxString & file);
        void onIdleEvent(wxIdleEvent & event);
private:
        void writeXmlStart(wxTextOutputStream & out);
        void writeHistrory(wxTextOutputStream & out, Buddy* buddy,const wxDateTime & since);
        void writeMessage(wxTextOutputStream & out, const wxDateTime & time, const wxString & message, bool outgoing);
        void writeBuddyStart(wxTextOutputStream & out, Buddy* buddy);
        wxString toBooleanString(bool b);
        wxString escapeString(wxString str);

        bool importStep();

    	static void wxCALLBACK addMainMenuItem(iMenu* menu, void* ptr);
    	void onImppCommand(ImppCommandEvent & event);

};


class StdImportExportModule : public iModule{


	DECLARE_DYNAMIC_CLASS(StdImportExportModule)

private:
	ImportExport* ie;

public:
	StdImportExportModule() :
		ie(NULL){};
	virtual ~StdImportExportModule(){};

	virtual void moduleInit(){
		ie = new ImportExport();
	};

	virtual void moduleDestroy(){
		wxDELETE(ie);
	}
};


}

#endif /*IMPORTEXPORT_H_*/

